{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying Task (Electron) Status in a Notebook\n",
    "\n",
    "Check the status of individual tasks in a workflow using the Covalent `Result` object.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Define and [run a workflow](../execution/execute_lattice.ipynb):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "import time\n",
    "\n",
    "@ct.electron\n",
    "def add(x, y):\n",
    "    time.sleep(5)\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def multiply(x, y):\n",
    "    return x * y\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(x, y):\n",
    "    res_1 = add(x=x, y=y)\n",
    "    return multiply(x=res_1, y=y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. View the transport graph in the Covalent GUI as described in the [User Interface Reference](../../webapp_ui/graph_view/index.html) to determine the node IDs for the electrons. \n",
    "2. Query the individual electrons' execution status using the node IDs from the transport graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Status(STATUS='NEW_OBJECT')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dispatch_id = ct.dispatch(workflow)(x=1, y=2)\n",
    "time.sleep(2)\n",
    "result = ct.get_result(dispatch_id=dispatch_id, wait=False)\n",
    "result.get_node_result(node_id=0)['status']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the query for the execution status via the `status` field. Possible values are `PENDING`, `FAILED`, `RUNNING`, or `COMPLETED`. A node can also have status `NEW_OBJECT` if the task has not yet been been put in a queue for execution."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Adding Electrons to Lattices](../coding/add_electron_to_lattice.ipynb)\n",
    "\n",
    "[Dispatching a Workflow](../execution/execute_lattice.ipynb)\n",
    "\n",
    "[Querying the Status of a Lattice](./query_lattice_execution_status.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
